import java.util.Arrays;

public class 前缀和Demo {
    
    public static void main(String[] args) {
        
        int[] nums = new int[]{1, 2, 3, 4, 5};

        int[] sums = preSums(nums);

        Arrays.stream(sums).boxed().map(s -> s+", ").forEach(System.out::print);
        System.out.println();

        System.out.println(sumInRange(sums, 0, sums.length - 2));

    }

    private static int[] preSums(int[] nums){

        if(null == nums ||  nums.length < 1) return null;
        int[] sums = new int[nums.length];

        sums[0] = nums[0];

        for(int i=1; i<nums.length; i++){

            sums[i] = sums[i-1] + nums[i];

        }

        return sums;
    }

    //计算区间和
    private static int sumInRange(int sums[], int left, int right){


        if(left==0) return sums[right];

        return sums[right] - sums[left - 1];

    }
}
